/*
 * Copyright (c) 2010 Mathew Hall, University of Sheffield.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * Neither the name of the University of Sheffield nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package search.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import primitives.cluster.ClusterHead;
import primitives.cluster.ClusterUtils;
import primitives.cluster.IClusterLevel;
import primitives.graph.Graph;
import primitives.graph.Node;
import primitives.util.UndefinedIndexException;

//foo bar baz
public abstract class SILReader {

    public static ClusterHead clusterTreeBySIL(Graph g, File input) throws IOException {
        ClusterHead ret = new ClusterHead(g);


        
        BufferedReader in = new BufferedReader(new FileReader(input));
        String line;
        HashMap<String, Node> nodes = new HashMap<String, Node>();
        for(Node n : g.getNodes())
            nodes.put(n.getLabel(),n);
        Pattern clusterDef = Pattern.compile("SS\\((SS.+)\\)=(.+)");

        HashMap<String,String> assignments = new HashMap<String,String>();
        HashSet<String> clusterNames = new HashSet<String>();
        HashMap<String,Node> clusterSeeds = new HashMap<String,Node>();

        while ((line = in.readLine()) != null) {
            Matcher m = clusterDef.matcher(line);
            if (m.matches()) {
                String[] assignedNodes = m.group(2).split(" |,");
                for(String nn : assignedNodes){
                    assignments.put(nn, m.group(1));
                    clusterSeeds.put(m.group(1), nodes.get(nn));
                }
                clusterNames.add(m.group(1));
                
            }
        }
        
        Logger.getLogger(SILReader.class.getName()).log(Level.INFO, String.format("Clusternames is %d",clusterNames.size()));

        for(String nodeName : assignments.keySet()){
              String targetCluster = assignments.get(nodeName);
            try{
              

                IClusterLevel home = ClusterUtils.lookup(nodes.get(nodeName), ret);
                IClusterLevel destination =
                        ClusterUtils.lookup
                            (clusterSeeds.get(targetCluster), ret);

                //Logger.getLogger(SILReader.class.getName()).log(Level.INFO, String.format("IDs for home: %s, destination:%s",home.getIDs(),destination.getIDs()));

                ClusterUtils.merge(home, destination, ret);
            }catch(UndefinedIndexException e){
                Logger.getLogger(SILReader.class.getName())
                        .log(Level.INFO, 
                            String.format("Failed with name=%s home=%s and target=%s dest=%s",
                            nodeName,
                            nodes.get(nodeName), targetCluster,
                            assignments.get(nodeName)), e);
            }

        }

        return ret;

        
    }
}
